package app.qrcode.browser.todoapp.fragments

import android.app.Application
import android.graphics.Typeface
import android.view.View
import android.widget.AdapterView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import app.qrcode.browser.todoapp.App.Companion.appContext
import app.qrcode.browser.todoapp.R
import app.qrcode.browser.todoapp.data.models.Priority
import app.qrcode.browser.todoapp.data.models.TaskStatus
import app.qrcode.browser.todoapp.data.models.ToDoData

/**
 * Created by YiGo on 2022/5/7.
 */
class SharedViewModel : ViewModel() {

    val emptyDatabase: MutableLiveData<Boolean> = MutableLiveData(false)


    fun checkIfDatabaseEmpty(toDoData: List<ToDoData>) {
        emptyDatabase.value = toDoData.isEmpty()
    }

    val listener: AdapterView.OnItemSelectedListener = object : AdapterView.OnItemSelectedListener {
        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            when (position) {
                0 -> {
                    (parent?.getChildAt(0) as TextView).setTextColor(
                        ContextCompat.getColor(
                            appContext,
                            R.color.red
                        )
                    )
                }
                1 -> {
                    (parent?.getChildAt(0) as TextView).setTextColor(
                        ContextCompat.getColor(
                            appContext,
                            R.color.yellow
                        )
                    )
                }
                2 -> {
                    (parent?.getChildAt(0) as TextView).setTextColor(
                        ContextCompat.getColor(
                            appContext,
                            R.color.green
                        )
                    )
                }
            }
        }


        override fun onNothingSelected(p0: AdapterView<*>?) {

        }

    }


    fun parsePriority(priority: String): Priority {
        return when (priority) {
            "High Priority" -> Priority.HIGH
            "Low Priority" -> Priority.LOW
            "Medium Priority" -> Priority.MEDIUM
            else -> Priority.LOW
        }
    }

    fun parseTaskStatus(taskStatus: String): TaskStatus {
        return when (taskStatus) {
            "To Do" -> TaskStatus.TODO
            "Qualify Instance" -> TaskStatus.QUALIFY_INSTANCE
            "Done" -> TaskStatus.DONE
            else -> TaskStatus.TODO
        }
    }

    fun verifyDataFromUser(title: String, description: String): Boolean {
        return !(title.isEmpty() || description.isEmpty())
    }


    fun parsePriority(priority: Priority): Int {
        return when (priority) {
            Priority.HIGH -> 0
            Priority.MEDIUM -> 1
            Priority.LOW -> 2
        }
    }

    fun parseTaskStatus(taskStatus: TaskStatus): Int {
        return when (taskStatus) {
            TaskStatus.TODO -> 0
            TaskStatus.QUALIFY_INSTANCE -> 1
            TaskStatus.DONE -> 2
        }
    }

    fun parseTaskStatusToString(taskStatus: TaskStatus): String {
        return when (taskStatus) {
            TaskStatus.TODO -> "To Do"
            TaskStatus.QUALIFY_INSTANCE -> "Quarlify Instance"
            TaskStatus.DONE -> "Done"
        }
    }
}
